package com.avic.rong.activity.mine;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.widget.ImageView;

import com.avic.rong.BaseActivity;
import com.avic.rong.R;
import com.avic.rong.fragment.MyFensFragment;
import com.avic.rong.questionOrAnswer.StringUtil;
import com.avic.rong.utils.SharedPreferencesUtil;
import com.flyco.tablayout.SlidingTabLayout;
import com.trello.rxlifecycle2.components.support.RxAppCompatActivity;

import java.util.ArrayList;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

//我的服务-我的关注粉丝
public class MyFansActivity extends BaseActivity {


    @BindView(R.id.img_return)
    ImageView img_return;
    @BindView(R.id.img_function)
    ImageView img_function;
    private ArrayList<Fragment> mFragments = new ArrayList<>();
    private String[] mTitles = null;
    @BindView(R.id.tablayout_fans)
    SlidingTabLayout mTablayout;
    @BindView(R.id.vp_fans)
    ViewPager vp_fans;
    private MyPagerAdapter myPagerAdapter;

    private int DEFAULT = 0;

    private String[] urls = {"content/followedUserList","content/followUserList"};
    private String homeUserID;


    @Override
    protected int getContentViewId() {
        return R.layout.activity_my_fans;
    }

    @Override
    protected void initView() {
        isNeedTitle = false;

        String userName = SharedPreferencesUtil.getInstance().getUserName();

        if (getIntent()!=null){
            DEFAULT = getIntent().getIntExtra("flag",0);
            homeUserID = getIntent().getStringExtra("homeUserID");
            String name = getIntent().getStringExtra("userName");
            if (userName.equals(name)){
                mTitles = new String[]{"我的关注", "我的粉丝"};
            }else {
                mTitles = new String[]{"TA的关注", "TA的粉丝"};
            }
        }

        myPagerAdapter = new MyPagerAdapter(getSupportFragmentManager(),mTitles);
        for (int i = 0; i < mTitles.length; i++) {
            mFragments.add(MyFensFragment.getInstence(mTitles[i], urls[i], this,homeUserID));
        }

        vp_fans.setAdapter(myPagerAdapter);
        mTablayout.setViewPager(vp_fans);
        vp_fans.setCurrentItem(DEFAULT);

    }

    @Override
    protected void addListener() {

    }

    @OnClick({R.id.img_return,R.id.img_function})
    public void onClick(View v) {
        switch (v.getId()) {
            default:
                break;
            case R.id.img_return:
                finish();
                break;
            case R.id.img_function:
                showPopup(MyFansActivity.this,findViewById(R.id.rl_root));
                break;
        }
    }

    private class MyPagerAdapter extends FragmentPagerAdapter {

        String[] titles;

        public MyPagerAdapter(FragmentManager fm, String[] titles) {
            super(fm);
            this.titles = titles;
        }


        @Override
        public int getCount() {
            return mFragments.size();
        }

        @Override
        public CharSequence getPageTitle(int position) {
            return titles[position];
        }

        @Override
        public Fragment getItem(int position) {
            return mFragments.get(position);
        }
    }

    public static Intent queryIntent(Context context, int flag,String homeUserID,String userName) {
        Intent intent = new Intent(context, MyFansActivity.class);
        intent.putExtra("flag", flag);
        intent.putExtra("homeUserID",homeUserID);
        intent.putExtra("userName",userName);
        return intent;
    }
}
